#include "EbnMonitor.h"
#include "dialog.h"
#include <QStringListModel>

CEbnMonitor::CEbnMonitor(CEbnModel * model)
{
    mpModel = model;
    mpCmList = new QStringListModel();
    mpGoalList = new QStringListModel();
    mpReceptorList = new QStringListModel();
    mpMonitorDialog = new Dialog();

    mpMonitorDialog->setCmListModel(mpCmList);
    mpMonitorDialog->setGoalListModel(mpGoalList);
    mpMonitorDialog->setReceptorListModel(mpReceptorList);

    connect(mpModel, SIGNAL(notifyUpdated()), this, SLOT(updateDisplay()));
}

CEbnMonitor::~CEbnMonitor()
{
    disconnect(mpModel, SIGNAL(notifyUpdated()), this, SLOT(updateDisplay()));

    if (mpMonitorDialog)
    {
        delete mpMonitorDialog;
        mpMonitorDialog = NULL;
    }

    mpModel = NULL;

    if (mpCmList)
    {
        delete mpCmList;
        mpCmList = NULL;
    }

    if (mpGoalList)
    {
        delete mpGoalList;
        mpGoalList = NULL;
    }

    if (mpReceptorList)
    {
        delete mpReceptorList;
        mpReceptorList = NULL;
    }

}

void CEbnMonitor::run(void)
{
    if (mpMonitorDialog)
    {
        mpMonitorDialog->show();
    }
}

void CEbnMonitor::stop(void)
{
    if (mpMonitorDialog)
    {
        mpMonitorDialog->hide();
    }
}

void CEbnMonitor::updateDisplay()
{
    QStringList mCmInfo;
    QStringList mGoalInfo;
    QStringList mReceptorInfo;

    int cmCnt = mpModel->getCompetenceModuleCount();
    int goalCnt = mpModel->getGoalCount();;
    int receptorCnt = mpModel->getReceptorCount();

    clearCmList();
    clearGoalList();
    clearReceptorList();

    for (int i = 0; i < cmCnt; i++)
    {
        mCmInfo.append(mpModel->getCompetenceModuleInfo(i));
    }

    setCmList(mCmInfo);

    for (int i = 0; i < goalCnt; i++)
    {
        mGoalInfo.append(mpModel->getGoalInfo(i));
    }

    setGoalList(mGoalInfo);

    for (int i = 0; i < receptorCnt; i++)
    {
        mReceptorInfo.append(mpModel->getReceptorInfo(i));
    }

    setReceptorList(mReceptorInfo);

    mpMonitorDialog->appendOutputMsg(mpModel->getModelInfo());
}

void CEbnMonitor::clearCmList(void)
{
    if (mpCmList)
    {
        mpCmList->stringList().clear();
    }
}

void CEbnMonitor::clearGoalList(void)
{
    if (mpGoalList)
    {
        mpGoalList->stringList().clear();
    }
}

void CEbnMonitor::clearReceptorList(void)
{
    if (mpReceptorList)
    {
        mpReceptorList->stringList().clear();
    }
}

void CEbnMonitor::setCmList(QStringList & cmInfo)
{
    if (mpCmList)
    {
        mpCmList->setStringList(cmInfo);
    }
}

void CEbnMonitor::setGoalList(QStringList & goalInfo)
{
    if (mpGoalList)
    {
        mpGoalList->setStringList(goalInfo);
    }
}

void CEbnMonitor::setReceptorList(QStringList & receptorInfo)
{
    if (mpReceptorList)
    {
        mpReceptorList->setStringList(receptorInfo);
    }
}

void CEbnMonitor::show()
{
    if (mpMonitorDialog)
    {
        mpMonitorDialog->show();
    }

}

void CEbnMonitor::hide()
{
    if (mpMonitorDialog)
    {
        mpMonitorDialog->hide();
    }

}
